package com.qianfeng.dbsm.admin.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qianfeng.dbsm.admin.common.MyUserContextHolder;
import com.qianfeng.dbsm.admin.entity.TbMember;
import com.qianfeng.dbsm.admin.service.TbMemberService;
import com.qianfeng.dbsm.admin.util.JWTUtil;
import com.qianfeng.dbsm.admin.vo.MemberMessageVo;
import com.qianfeng.dbsm.common.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 会员表(TbMember)表控制层
 *
 * @author makejava
 * @since 2022-11-14 19:49:03
 */
@RestController
@RequestMapping("tbMember")
@CrossOrigin
public class TbMemberController{

    @Autowired
    TbMemberService tbMemberService;

    @ApiOperation(value = "JWT会员登录-login")
    @PostMapping("login.do")
    public ResponseData login(String username, String password){

        TbMember member = tbMemberService.login(username,password);
        //生成短token
        String token = JWTUtil.create(username);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("token",token);
        //返回token给浏览器
        ResponseData responseData = ResponseData.success("登录成功",map);

        //生成长token
        String ctoken = JWTUtil.create2(username);
        //将长短token放入tokenmap中
        JWTUtil.put(token,ctoken);
        return responseData;

    }

    @ApiOperation(value = "security的用户登录")
    @PostMapping("/login.action")
    public void loginSecurity(String username,String password){

    }


    @ApiOperation(value = "security的用户退出登录--并不完美")
    @PostMapping("/logout")
    public void logout(HttpServletRequest httpServletRequest){
//        String token = httpServletRequest.getHeader("Authorization");
//        System.out.println("进来了吗？？？？？？？？？？？？？？？？？？？？？？？？？？");
//        JWTUtil.logout(token);
//        return ResponseData.success("退出登录成功",null);
    }


    @ApiOperation(value = "查看登录用户信息")
    @PostMapping("info")
    public ResponseData info(){

        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        TbMember loginUser = tbMemberService.getOne(new QueryWrapper<TbMember>().eq("member_account",userDetails.getUsername()));

        return ResponseData.success(null,loginUser);


    }




}

